<?php
include("template_top.php");
$sql = "SELECT * FROM `tbReports` WHERE `sUname` = :uname AND `iWeek` = :week AND `iYear` = :year";
$q = db::Query($sql, array(':uname'=>$USER['sUname'], ':week'=>date("W"), ':year'=>date("Y")));
$REPORT = $q->fetch();
if(isset($_GET['p'])){
	$sql = "DELETE FROM `tbProjects` WHERE `iID` = :id";
	db::Query($sql, array(':id'=>$_GET['p']));
}
if(isset($_GET['t'])){
	$sql = "DELETE FROM `tbTasks` WHERE `iID` = :id";
	db::Query($sql, array(':id'=>$_GET['t']));
}
if(isset($_POST['action'])){
	if($_POST['action'] == "final"){
		if($REPORT['sUname'] == NULL){
			$sql = "INSERT INTO `tbReports` VALUES(null, :uname, :week, :year, null, 'Complete')";
			db::Query($sql, array(':uname'=>$USER['sUname'], ':week'=>date("W"), ':year'=>date("Y")));
		}else{
			$sql = "UPDATE `tbReports` SET `sStatus` = 'Complete' WHERE `sUname` = :uname AND `iWeek` = :week AND `iYear` = :year";
			db::Query($sql, array(':uname'=>$USER['sUname'], ':week'=>date("W"), ':year'=>date("Y")));
		}
	}
	if($_POST['action'] == "save_report"){
		if($REPORT['sUname'] == NULL){
			$sql = "INSERT INTO `tbReports` VALUES(null, :uname, :week, :year, :comments, 'In Progress')";
			db::Query($sql, array(':uname'=>$USER['sUname'], ':week'=>date("W"), ':year'=>date("Y"), ':comments'=>$_POST['comments']));
		}else{
			$sql = "UPDATE `tbReports` SET `sComments` = :comments WHERE `sUname` = :uname AND `iWeek` = :week AND `iYear` = :year";
			db::Query($sql, array(':uname'=>$USER['sUname'], ':week'=>date("W"), ':year'=>date("Y"), ':comments'=>$_POST['comments']));
		}
		$c = 1;
		while($c <= 10){
			if($_POST['projectSummary'.$c] != ''){
				$sql = "SELECT * FROM `tbProjects` WHERE `sSummary` = :sum AND `sUname` = :uname";
				$q = db::Query($sql, array(':sum'=>$_POST['projectSummary'.$c], ':uname'=>$USER['sUname']));
				$r = $q->fetch();
				if($r['iID'] == NULL){
					$sql = "INSERT INTO `tbProjects` VALUES (null, :uname, :sum, :assigned, :status)";
					db::Query($sql, array(':uname'=>$USER['sUname'], ':sum'=>$_POST['projectSummary'.$c], ':assigned'=>$_POST['projectAssigned'.$c], ':status'=>$_POST['projectStatus'.$c]));
				}else{
					$sql = "UPDATE `tbProjects` SET `sAssigned` = :assigned, `sStatus` = :status WHERE `iID` = :id";
					db::Query($sql, array(':assigned'=>$_POST['projectAssigned'.$c], ':status'=>$_POST['projectStatus'.$c], ':id'=>$r['iID']));
				}
			}
			
			if($_POST['taskSummary'.$c] != ''){
				$sql = "INSERT INTO `tbTasks` VALUES (null, :uname, :sum, 'Not Reported', 'Dean')";
				db::Query($sql, array(':uname'=>$USER['sUname'], ':sum'=>$_POST['taskSummary'.$c]));
			}
			
			if($_POST['taskSamSummary'.$c] != ''){
				$sql = "INSERT INTO `tbTasks` VALUES (null, :uname, :sum, 'Not Reported', 'Sam')";
				db::Query($sql, array(':uname'=>$USER['sUname'], ':sum'=>$_POST['taskSamSummary'.$c]));
			}
			$c ++;
		}
	}
}
$sql = "SELECT * FROM `tbReports` WHERE `sUname` = :uname AND `iWeek` = :week AND `iYear` = :year";
$q = db::Query($sql, array(':uname'=>$USER['sUname'], ':week'=>date("W"), ':year'=>date("Y")));
$REPORT = $q->fetch();
?>
<h3>Weekly Parature Report</h3>
<?php if($REPORT['sStatus'] != "Complete"){ ?>
<div class="warning">
Remember your report will not be sent until 8:30 PM on Friday.<br/>
If your report is not finalized by 8:30 PM on Friday, your report will be sent as is.
</div>
<center>
<?php if(date("N") == 5 or date("N") == 4){ ?>
<div class="info">
Click The Button Below to Finalize Your Report<br/>Once You Finalize You Will NOT be Able to Edit The Report
</div>
<form action="Weekly_Parature_Report.php" method="post">
<input type="submit" value=" Finalize Report " style="font-size:18px; font-weight:bold"/>
<input type="hidden" name="action" value="final" />
</form>
<?php }else{ ?>
<input type="button" value=" Reports Can Only Be Finalized on Thursdays or Fridays " style="font-size:18px; font-weight:bold" disabled />
<?php } ?>
</center>
<?php }else{ ?>
<?php if(date("N") > 5){ ?>
	<div class="warning">
	Your report was successfully sent at 8:30 PM on Friday
	</div>
<?php }else{ ?>
	<?php if(date("N") == 5){ ?>
    	<?php if(date("G") >= 18){ ?>
			<div class="warning">
			Your report was successfully sent at 8:30 PM on Friday
			</div>
        <?php }else{ ?>
			<div class="warning">
			Your report has already been finalized, and is scheduled to be sent today at 8:30 PM
			</div>
        <?php } ?>
    <?php }else{ ?>
		<?php if(date("N") == 4){ ?>
			<div class="warning">
			Your report has already been finalized, and is scheduled to be sent tomorrow at 8:30 PM
			</div>
        <?php }else{ ?>
			<div class="warning">
			Your report has already been finalized, and is scheduled to be sent on Friday at 8:30 PM
			</div>
        <?php } ?>
	<?php } ?>
<?php } ?>
<?php } ?>
<br/><br/>
<form action="Weekly_Parature_Report.php" method="POST">
<h3>Prepare Weekly Parature Report</h3>
<h4>Parature Details:</h4>
Open Tickets on Monday: <?php echo $PARATURE['iTicketsMonday'] ?><br/>
Open Tickets Currently: <?php echo $PARATURE['iTicketsOpen'] ?><br/>
Tickets Completed This Week: <?php echo $PARATURE['iTicketsCompleteThisWeek'] ?><br><br>
<h4>Projects:</h4>
<table width="100%">
	<tr>
    	<td>
        <b>Project Summary</b>
        </td>
        <td width="150" align="center">
        <b>Project Assigned By</b>
        </td>
        <td width="150" align="center">
        <b>Project Status</b>
        </td>
	</tr>
    <?php
	$c = 1;
	$shown = 20;
	$sql = "SELECT * FROM `tbProjects` WHERE `sUname` = :uname AND `sStatus` != 'Reported'";
	$q = db::Query($sql, array(':uname'=>$USER['sUname']));
	$currentProjects = array();
	while($row = $q->fetch()){
		$currentProjects[] = array('ID'=>$row['iID'], 'Summary'=>$row['sSummary'], 'Assigned'=>$row['sAssigned'], 'Status'=>$row['sStatus']);
	}
	if($REPORT['sStatus'] != "Complete"){
		$con = false;
	}else{
		$con = true;
	}
	while($c <= 20){
	?>
    <tr id="project<?php echo $c ?>" <?php if($c != 1 and $currentProjects[$c - 1] == NULL and $con == true){ ?>style="display:none"<?php $shown -- ?><?php } ?>>
    	<td>
    	<?php if($currentProjects[$c - 1] != NULL){ ?>
        <a href="Weekly_Parature_Report.php?p=<?php echo $currentProjects[$c - 1]['ID'] ?>"><img src="images/delete.png" align="left" style="float:left" width="16" /></a>
        <?php } ?>
        <input type="text" name="projectSummary<?php echo $c ?>" style="width:<?php if($currentProjects[$c - 1] != NULL){ ?>94<?php }else{ ?>98<?php } ?>%; float:left" value="<?php echo $currentProjects[$c - 1]['Summary'] ?>" <?php if($REPORT['sStatus'] == 'Complete' or $currentProjects[$c - 1] != NULL){ ?>disabled<?php } ?> >
        <?php if($currentProjects[$c - 1] != NULL){ ?>
        <input type="hidden" name="projectSummary<?php echo $c ?>" value="<?php echo $currentProjects[$c - 1]['Summary'] ?>">
        <?php } ?>
        </td>
        <td align="center">
        <select name="projectAssigned<?php echo $c ?>" <?php if($REPORT['sStatus'] == 'Complete'){ ?>disabled="disabled"<?php } ?>>
        	<option value="">-- SELECT ONE --</option>
            <option <?php if($currentProjects[$c - 1]['Assigned'] == "Dean Phetterplace"){echo "selected";} ?>>Dean Phetterplace</option>
            <option <?php if($currentProjects[$c - 1]['Assigned'] == "Sam Flecha"){echo "selected";} ?>>Sam Flecha</option>
            <option <?php if($currentProjects[$c - 1]['Assigned'] == "Tyla Bebon"){echo "selected";} ?>>Tyla Bebon</option>
            <option <?php if($currentProjects[$c - 1]['Assigned'] == "Other"){echo "selected";} ?>>Other</option>
        </select>
        </td>
        <td align="center">
        <select name="projectStatus<?php echo $c ?>" <?php if($REPORT['sStatus'] == 'Complete'){ ?>disabled="disabled"<?php } ?>>
            <option <?php if($currentProjects[$c - 1]['Status'] == "New"){echo "selected";} ?>>New</option>						
            <option <?php if($currentProjects[$c - 1]['Status'] == "In Progress"){echo "selected";} ?>>In Progress</option>
            <option <?php if($currentProjects[$c - 1]['Status'] == "Awaiting Feedback"){echo "selected";} ?>>Awaiting Feedback</option>
            <option <?php if($currentProjects[$c - 1]['Status'] == "Finished"){echo "selected";} ?>>Finished</option>
        </select>
        </td>
	</tr>
    <?php 
		if($currentProjects[$c - 1] == NULL){
			$con = true;
		}
	$c ++;
	}
	?>
    <script>
	var currentNumber = <?php echo $shown ?>;
	function addProject(){
		if(currentNumber != 20){
			currentNumber ++;
			document.getElementById('project'+currentNumber).style.display = 'table-row';
		}else{
			alert("The system is only cabable of 20 projects at this time.\n\nPlease add the other projects to the comments section.");
		}
		return false;
	}
	</script>
    <?php if($REPORT['sStatus'] != "Complete"){ ?>
    <tr>
    	<td colspan="3">
        <a href="#" onClick="return addProject()">Add New Project</a>
        </td>
	</tr>      
    <?php } ?> 
</table><br><br>
<h4>Dean Tasks (Things Dean Nextelled/Emailed You To Do):</h4>
<table width="100%">
	<tr>
    	<td>
        <b>Task Summary</b>
        </td>
	</tr>
    <?php
	$c = 1;
	$shown = 20;
	$sql = "SELECT * FROM `tbTasks` WHERE `sUname` = :uname AND `sStatus` != 'Reported' AND `sAssigned` = 'Dean'";
	$q = db::Query($sql, array(':uname'=>$USER['sUname']));
	$currentTasks = array();
	while($row = $q->fetch()){
		$currentTasks[] = array('Summary'=>$row['sSummary'], 'ID'=>$row['iID']);
	}
	if($REPORT['sStatus'] != "Complete"){
		$con = false;
	}else{
		$con = true;
	}
	while($c <= 20){
	?>
    <tr id="task<?php echo $c ?>" <?php if($c != 1 and $currentTasks[$c - 1] == NULL and $con == true){ ?>style="display:none"<?php $shown -- ?><?php } ?>>
    	<td>
		<?php if($currentTasks[$c - 1] != NULL){ ?>
        <a href="Weekly_Parature_Report.php?t=<?php echo $currentTasks[$c - 1]['ID'] ?>"><img src="images/delete.png" align="left" style="float:left" width="16" /></a>
        <?php } ?>
        <input type="text" name="taskSummary<?php echo $c ?>" style="width:<?php if($currentTasks[$c - 1] != NULL){ ?>94<?php }else{ ?>98<?php } ?>%" value="<?php echo $currentTasks[$c - 1]['Summary'] ?>"<?php if($REPORT['sStatus'] == 'Complete' or $currentTasks[$c - 1] != NULL){ ?>disabled<?php } ?>>
        </td>
	</tr>
    <?php 
		if($currentTasks[$c - 1] == NULL){
			$con = true;
		}
		$c ++;
	}
	?>
    <script>
	var currentTaskNumber = <?php echo $shown ?>;
	function addTask(){
		if(currentTaskNumber != 20){
			currentTaskNumber ++;
			document.getElementById('task'+currentTaskNumber).style.display = 'table-row';
		}else{
			alert("The system is only cabable of 20 tasks at this time.\n\nPlease add the other tasks to the comments section.");
		}
		return false;
	}
	</script>
    <?php if($REPORT['sStatus'] != "Complete"){ ?>
    <tr>
    	<td colspan="3">
        <a href="#" onClick="return addTask()">Add New Task</a>
        </td>
	</tr>       
    <?php } ?>
</table><br><br>
<h4>Sam Tasks (Things Sam Nextelled/Emailed You To Do):</h4>
<table width="100%">
	<tr>
    	<td>
        <b>Task Summary</b>
        </td>
	</tr>
    <?php
	$c = 1;
	$shown = 20;
	$sql = "SELECT * FROM `tbTasks` WHERE `sUname` = :uname AND `sStatus` != 'Reported' AND `sAssigned` = 'Sam'";
	$q = db::Query($sql, array(':uname'=>$USER['sUname']));
	$currentTasksSam = array();
	while($row = $q->fetch()){
		$currentTasksSam[] = array('Summary'=>$row['sSummary'], 'ID'=>$row['iID']);
	}
	if($REPORT['sStatus'] != "Complete"){
		$con = false;
	}else{
		$con = true;
	}
	while($c <= 20){
	?>
    <tr id="taskSam<?php echo $c ?>" <?php if($c != 1 and $currentTasksSam[$c - 1] == NULL and $con == true){ ?>style="display:none"<?php $shown -- ?><?php } ?>>
    	<td>
		<?php if($currentTasksSam[$c - 1] != NULL){ ?>
        <a href="Weekly_Parature_Report.php?t=<?php echo $currentTasksSam[$c - 1]['ID'] ?>"><img src="images/delete.png" align="left" style="float:left" width="16" /></a>
        <?php } ?>
        <input type="text" name="taskSamSummary<?php echo $c ?>" style="width:<?php if($currentTasksSam[$c - 1] != NULL){ ?>94<?php }else{ ?>98<?php } ?>%" value="<?php echo $currentTasksSam[$c - 1]['Summary'] ?>"<?php if($REPORT['sStatus'] == 'Complete' or $currentTasksSam[$c - 1] != NULL){ ?>disabled<?php } ?>>
        </td>
	</tr>
    <?php 
		if($currentTasksSam[$c - 1] == NULL){
			$con = true;
		}
		$c ++;
	}
	?>
    <script>
	var currentTaskSamNumber = <?php echo $shown ?>;
	function addTaskSam(){
		if(currentTaskSamNumber != 20){
			currentTaskSamNumber ++;
			document.getElementById('taskSam'+currentTaskSamNumber).style.display = 'table-row';
		}else{
			alert("The system is only cabable of 20 tasks at this time.\n\nPlease add the other tasks to the comments section.");
		}
		return false;
	}
	</script>
    <?php if($REPORT['sStatus'] != "Complete"){ ?>
    <tr>
    	<td colspan="3">
        <a href="#" onClick="return addTaskSam()">Add New Task</a>
        </td>
	</tr>       
    <?php } ?>
</table><br><br>
<h4>Comments:</h4>
<textarea name="comments" style="width:98%; height:200px" <?php if($REPORT['sStatus'] == 'Complete'){ ?>disabled<?php } ?>><?php echo $REPORT['sComments'] ?></textarea>
<center>
<input type="submit" value=" Save Report " style="font-size:18px; font-weight:bold" <?php if($REPORT['sStatus'] == 'Complete'){ ?>disabled<?php } ?>/>
<input type="hidden" name="action" value="save_report" />
</center>
</form>
<?php
include("template_bot.php");
?>